#include "DNSResponder.h"


void DNSResponder::writeResponse(ether_header* ether, ip* ipp, udphdr* udp, HEADER* dns, u_char* data, char* name, int datalen)
{
	int pllen = 2*(datalen+5)+10;
	u_char* pl = new u_char[pllen];
	memcpy(pl,data,datalen+5);
	memcpy(pl+datalen+5,data,datalen+5);
	memcpy(pl+2*(datalen+5),"\x00\x00\x00\x00\x00\x04",6);
	*((u_long*)(pl+2*(datalen+5)+6)) = ipaddr;
	in_addr_t src = inet_addr(destination->c_str());
	libnet_ptag_t ret;
	ret = libnet_build_dnsv4(LIBNET_UDP_DNSV4_H,
			ntohs(dns->id),
			0x8580,
			1,
			1,
			0,
			0,
			pl,
			pllen,
			lnet,
			0);
	if (ret == -1)
	{
		cout << libnet_geterror(this->lnet) << endl;
		exit(1);
	}
	ret = libnet_build_udp(udp->dest,
			udp->source,
			LIBNET_UDP_H + pllen,
			0,
			pl,
			pllen+12,
			lnet,
			0);
	if (ret == -1)
	{
		cout << libnet_geterror(this->lnet) << endl;
		exit(1);
	}
	ret = libnet_build_ipv4(LIBNET_UDP_H + LIBNET_UDP_DNSV4_H,
			0,
			0,
			0,
			64,
			IPPROTO_UDP,
			0,
			src,
			ipp->ip_src.s_addr,
			NULL,
			0,
			lnet,
			0);
	if (ret == -1)
	{
		cout << libnet_geterror(this->lnet) << endl;
		exit(1);
	}
	write();
	cout << "wrote packet\n";
	delete [] pl;
}
